package com.aiyige.page.pay.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.AgreementBackup;
import com.aiyige.model.OrderEntity;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.backup.MediaSummary;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.publish.guarantee.model.GuaranteeItem;
import com.aiyige.utils.ListUtil;

import java.util.List;

/**
 * Created by zhuyakun on 2018/1/29.
 */

public class OrderModel implements Parcelable {
    String orderId;
    String title;
    String coverUrl;
    long buyNum;
    String goodsId;
    String sellerId;
    String sellerName;
    String sellerAvatar;
    String subject;
    int subjectIndex;
    double singlePrice;
    double totalPrice;
    double totalPayPrice;
    String source;
    String sourceUrl;
    List<GuaranteeItem> guaranteeItemList;
    int version;
    Double lineSinglePrice;

    String contentCountString;

    public OrderModel() {
    }


    private OrderModel(Builder builder) {
        setOrderId(builder.orderId);
        setTitle(builder.title);
        setCoverUrl(builder.coverUrl);
        setBuyNum(builder.buyNum);
        setGoodsId(builder.goodsId);
        setSellerId(builder.sellerId);
        setSellerName(builder.sellerName);
        setSellerAvatar(builder.sellerAvatar);
        setSubject(builder.subject);
        setSubjectIndex(builder.subjectIndex);
        setSinglePrice(builder.singlePrice);
        setTotalPrice(builder.totalPrice);
        setTotalPayPrice(builder.totalPayPrice);
        setSource(builder.source);
        setSourceUrl(builder.sourceUrl);
        setGuaranteeItemList(builder.guaranteeItemList);
        setVersion(builder.version);
        setLineSinglePrice(builder.lineSinglePrice);
        setContentCountString(builder.contentCountString);
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public long getBuyNum() {
        return buyNum;
    }

    public void setBuyNum(long buyNum) {
        this.buyNum = buyNum;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getSellerId() {
        return sellerId;
    }

    public void setSellerId(String sellerId) {
        this.sellerId = sellerId;
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public String getSellerAvatar() {
        return sellerAvatar;
    }

    public void setSellerAvatar(String sellerAvatar) {
        this.sellerAvatar = sellerAvatar;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public int getSubjectIndex() {
        return subjectIndex;
    }

    public void setSubjectIndex(int subjectIndex) {
        this.subjectIndex = subjectIndex;
    }

    public double getSinglePrice() {
        return singlePrice;
    }

    public void setSinglePrice(double singlePrice) {
        this.singlePrice = singlePrice;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public double getTotalPayPrice() {
        return totalPayPrice;
    }

    public void setTotalPayPrice(double totalPayPrice) {
        this.totalPayPrice = totalPayPrice;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public List<GuaranteeItem> getGuaranteeItemList() {
        return guaranteeItemList;
    }

    public void setGuaranteeItemList(List<GuaranteeItem> guaranteeItemList) {
        this.guaranteeItemList = guaranteeItemList;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getContentCountString() {
        return contentCountString == null ? "" : contentCountString;
    }

    public void setContentCountString(String contentCountString) {
        this.contentCountString = contentCountString == null ? "" : contentCountString;
    }

    public Double getLineSinglePrice() {
        return lineSinglePrice;
    }

    public void setLineSinglePrice(Double lineSinglePrice) {
        this.lineSinglePrice = lineSinglePrice;
    }



    public static String getContentValue(OrderEntity orderEntity) {
        String content = null;
        if (orderEntity == null) {
            return content;
        }
        // 专业课程
        AgreementBackup agreementBackup = ListUtil.isEmpty(orderEntity.getAgreementBackup()) ? null : orderEntity.getAgreementBackup().get(0);
        if (orderEntity.getGoodsType() != null && orderEntity.getGoodsType().equals(Subject.major_course)) {
            if (agreementBackup != null && !TextUtils.isEmpty(agreementBackup.getMoreBackup())) {
                MajorCourse majorCourse = JsonUtil.toObject(agreementBackup.getMoreBackup(), MajorCourse.class);
                if (majorCourse != null) {
                    content = MyApp.getAppContext().getString(R.string.class_count);
                }
            }
        } else if (orderEntity.getGoodsType() != null && (orderEntity.getGoodsType().equals(Subject.video_course) || orderEntity.getGoodsType().equals(Subject.video))) {
            content = MyApp.getAppContext().getString(R.string.content_count_value, orderEntity.getVideoStages() + "");
        }
        return content;
    }

    public static String getContentValue(Moment moment) {
        String content = null;
        if (moment == null) {
            return content;
        }
        // 专业课程
        if (moment.getSubject() != null && moment.getSubject().equals(Subject.major_course)) {
            if (!TextUtils.isEmpty(moment.getMoreBackup())) {
                MajorCourse majorCourse = JsonUtil.toObject(moment.getMoreBackup(), MajorCourse.class);
                if (majorCourse != null) {
                    content = majorCourse.getClassCount() + MyApp.getAppContext().getString(R.string.class_count);
                }
            }
        } else if (moment.getSubject() != null && moment.getSubject().equals(Subject.video_course) || (moment.getSubject().equals(Subject.video)))
        {
            MediaSummary mediaSummary = moment.getMediaSummary();
            if (mediaSummary != null) {
                content = MyApp.getAppContext().getString(R.string.content_count_value, mediaSummary.getCount() + "");
            }
        }
        return content;
    }

    public static final class Builder {
        private String orderId;
        private String title;
        private String coverUrl;
        private long buyNum;
        private String goodsId;
        private String sellerId;
        private String sellerName;
        private String sellerAvatar;
        private String subject;
        private int subjectIndex;
        private double singlePrice;
        private double totalPrice;
        private double totalPayPrice;
        private String source;
        private String sourceUrl;
        private List<GuaranteeItem> guaranteeItemList;
        private int version;
        private Double lineSinglePrice;
        private String contentCountString;

        private Builder() {
        }

        public Builder orderId(String val) {
            orderId = val;
            return this;
        }

        public Builder title(String val) {
            title = val;
            return this;
        }

        public Builder coverUrl(String val) {
            coverUrl = val;
            return this;
        }

        public Builder buyNum(long val) {
            buyNum = val;
            return this;
        }

        public Builder goodsId(String val) {
            goodsId = val;
            return this;
        }

        public Builder sellerId(String val) {
            sellerId = val;
            return this;
        }

        public Builder sellerName(String val) {
            sellerName = val;
            return this;
        }

        public Builder sellerAvatar(String val) {
            sellerAvatar = val;
            return this;
        }

        public Builder subject(String val) {
            subject = val;
            return this;
        }

        public Builder subjectIndex(int val) {
            subjectIndex = val;
            return this;
        }

        public Builder singlePrice(double val) {
            singlePrice = val;
            return this;
        }

        public Builder totalPrice(double val) {
            totalPrice = val;
            return this;
        }

        public Builder totalPayPrice(double val) {
            totalPayPrice = val;
            return this;
        }

        public Builder source(String val) {
            source = val;
            return this;
        }

        public Builder sourceUrl(String val) {
            sourceUrl = val;
            return this;
        }

        public Builder guaranteeItemList(List<GuaranteeItem> val) {
            guaranteeItemList = val;
            return this;
        }

        public Builder version(int val) {
            version = val;
            return this;
        }

        public Builder lineSinglePrice(Double val) {
            lineSinglePrice = val;
            return this;
        }

        public Builder contentCountString(String val) {
            contentCountString = val;
            return this;
        }

        public OrderModel build() {
            return new OrderModel(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.orderId);
        dest.writeString(this.title);
        dest.writeString(this.coverUrl);
        dest.writeLong(this.buyNum);
        dest.writeString(this.goodsId);
        dest.writeString(this.sellerId);
        dest.writeString(this.sellerName);
        dest.writeString(this.sellerAvatar);
        dest.writeString(this.subject);
        dest.writeInt(this.subjectIndex);
        dest.writeDouble(this.singlePrice);
        dest.writeDouble(this.totalPrice);
        dest.writeDouble(this.totalPayPrice);
        dest.writeString(this.source);
        dest.writeString(this.sourceUrl);
        dest.writeTypedList(this.guaranteeItemList);
        dest.writeInt(this.version);
        dest.writeValue(this.lineSinglePrice);
        dest.writeString(this.contentCountString);
    }

    protected OrderModel(Parcel in) {
        this.orderId = in.readString();
        this.title = in.readString();
        this.coverUrl = in.readString();
        this.buyNum = in.readLong();
        this.goodsId = in.readString();
        this.sellerId = in.readString();
        this.sellerName = in.readString();
        this.sellerAvatar = in.readString();
        this.subject = in.readString();
        this.subjectIndex = in.readInt();
        this.singlePrice = in.readDouble();
        this.totalPrice = in.readDouble();
        this.totalPayPrice = in.readDouble();
        this.source = in.readString();
        this.sourceUrl = in.readString();
        this.guaranteeItemList = in.createTypedArrayList(GuaranteeItem.CREATOR);
        this.version = in.readInt();
        this.lineSinglePrice = (Double) in.readValue(Double.class.getClassLoader());
        this.contentCountString = in.readString();
    }

    public static final Creator<OrderModel> CREATOR = new Creator<OrderModel>() {
        @Override
        public OrderModel createFromParcel(Parcel source) {
            return new OrderModel(source);
        }

        @Override
        public OrderModel[] newArray(int size) {
            return new OrderModel[size];
        }
    };

    public double getCurrentPrice() {
        return lineSinglePrice != null ? lineSinglePrice : getSinglePrice();
    }

    public boolean hasLinePrice() {
        return lineSinglePrice != null;
    }
}
